/**
 * @file main3.cpp
 * @author your name (you@domain.com)
 * @brief  Winsock的实例代码 udp通信的服务器方/发送方
 * @version 0.1
 * @date 2022-03-02
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include<WINSOCK2.h>
#include<IOSTREAM>
#pragma comment(lib,"ws2_32.lib")
using namespace std;

int main(){

    //init DLL
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2,2),&wsadata);
    SOCKET serversocker = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    //IPV4,   liushichuanshu  tcpxieyi
    //绑定套接字
    sockaddr_in socketaddr ;
    //保存其他信息
    ZeroMemory(&socketaddr,sizeof(socketaddr));
    socketaddr.sin_family= PF_INET;
    socketaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    socketaddr.sin_port = htons (1234);
    bind(serversocker,(sockaddr *) &socketaddr,sizeof(socketaddr));

    //进入监听状态
    listen(serversocker,20);
    //接受客户端请求
    SOCKADDR clinetaddr;
    int size = sizeof(clinetaddr);

    SOCKET clientsocket = accept(serversocker,(SOCKADDR*)&clinetaddr,&size);

    //向客户端发送简单信息
    cout<<"Connect succfully ! Ready to send message !"<<endl;

    const char *message = "Hello world!"; 

    send(clientsocket,message,(int)strlen(message),0);

    // 关闭套接字
    closesocket(clientsocket);
    closesocket(serversocker);


    //停止使用 DLL
    WSACleanup();
    cin.get();
    cin.get();
    return 0;
    


}
